package com.art8173;

import java.util.ArrayList;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> accounts = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    private Account loginAcc;

    /**
     * 启动ATM，启动后进入登录界面
     */
    public void start() {
        while (true) {
            System.out.println("==欢迎来到ATM系统==");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("请选择：");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    // 登录
                    login();
                    break;
                case "2":
                    // 注册
                    register();
                    break;
                default:
                    System.out.println("输入错误，请重新输入！");
                    start();
            }
        }
    }

    private void login() {
        if (accounts.isEmpty()) {
            System.out.println("没有用户，请先注册！");
            return;
        }
        while (true) {
            System.out.println("请输入卡号：");
            String CardID = sc.next();
            Account acc = queryByCardId(CardID);
            if (acc == null) {
                System.out.println("卡号不存在！");
            } else {
                while (true) {
                    System.out.println("请输入密码：");
                    String password = sc.next();
                    if (acc.getPassword().equals(password)) {
                        loginAcc = acc;
                        System.out.println("登录成功！");
                        //展示登录后的操作页面
                        showUserCommand();
                        return;
                    } else {
                        System.out.println("密码错误！");
                    }
                }
            }
        }
    }

    private void showUserCommand() {
        while (true) {
            System.out.println("您可选择如下处理：");
            System.out.println("1.查询余额");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    showInfo();
                    break;
                case 2:
                    deposit();
                    break;
                case 3:
                    withdraw();
                    break;
                case 4:
                    transfer();
                    break;
                case 5:
                    changePassword();
                    return;
                case 6:
                    System.out.println("退出系统");
                    return;
                case 7:
                    if (deleteAcc()) {
                        return;
                    }
                    break;
                default:
                    System.out.println("输入错误！");
                    break;
            }
        }
    }

    private void changePassword() {
        while (true) {
            System.out.println("请认证当前密码：");
            String password = sc.next();
            if (!password.equals(loginAcc.getPassword())) {
                System.out.println("密码错误！");
            } else {
                while (true) {
                    System.out.println("请输入新密码：");
                    String newPassword = sc.next();
                    System.out.println("请输入确认密码：");
                    String confirmPassword = sc.next();
                    if (!newPassword.equals(confirmPassword)) {
                        System.out.println("两次密码不一致！");
                    } else {
                        loginAcc.setPassword(newPassword);
                        System.out.println("修改密码成功！");
                        return;
                    }
                }

            }
        }
    }

    private boolean deleteAcc() {
        System.out.println("请问确认销户吗？y/n");
        String choice = sc.next();
        if (choice.equals("y")) {
            if (loginAcc.getMoney() > 0) {
                System.out.println("账户有余额，请先取款");
                return false;
            } else {
                System.out.println("销户成功！");
                accounts.remove(loginAcc);
                return true;
            }
        } else {
            System.out.println("账户保留");
            return false;
        }
    }

    private void transfer() {
        if (accounts.size() < 2) {
            System.out.println("没有其他账户！");
            return;
        }
        if (loginAcc.getMoney() == 0) {
            System.out.println("一毛钱也没！");
            return;
        }
        while (true) {
            System.out.println("请输入对方卡号：");
            String cardID = sc.next();
            Account targetAcc = queryByCardId(cardID);
            if (targetAcc == null) {
                System.out.println("未找到该用户！");
            } else if (targetAcc.equals(loginAcc)) {
                System.out.println("不能给自己转账！");
            } else {
                //验证对方姓氏
                String name = "*" + targetAcc.getUserName().charAt(0);
                System.out.println("请输入" + name + "姓氏：");
                String preName = sc.next();
                if (targetAcc.getUserName().startsWith(preName)) {

                    System.out.println("请输入转账金额：");
                    double money = sc.nextDouble();
                    if (money > loginAcc.getMoney()) {
                        System.out.println("余额不足！，最多可以转：" + loginAcc.getMoney());
                    } else {
                        loginAcc.setMoney(loginAcc.getMoney() - money);
                        targetAcc.setMoney(targetAcc.getMoney() + money);
                        System.out.println("转账成功！");
                        return;
                    }
                } else {
                    System.out.println("请输入正确的姓氏！");
                }

            }
        }

    }

    private void withdraw() {
        if (loginAcc.getMoney() < 100) {
            System.out.println("账户不足100，取不了");
            return;
        }
        while (true) {
            System.out.println("请输入取款金额：");
            double money = sc.nextDouble();
            if (money <= loginAcc.getMoney()) {
                if (money > loginAcc.getLimit()) {
                    System.out.println("超出取款额度");
                } else {
                    System.out.println("取款成功");
                    loginAcc.setMoney(loginAcc.getMoney() - money);
                    return;
                }
            } else {
                System.out.println("余额不足，您的账户余额为：" + loginAcc.getMoney());
            }
        }

    }

    private void deposit() {
        System.out.println("请输入存款金额：");
        double money = sc.nextDouble();
        loginAcc.setMoney(loginAcc.getMoney() + money);
        System.out.println("存款成功！存钱后余额：" + loginAcc.getMoney());
    }

    //展示当前账户信息
    private void showInfo() {
        System.out.println("当前您的账户如下：");
        System.out.println("卡号：" + loginAcc.getCardID());
        System.out.println("用户名：" + loginAcc.getUserName());
        System.out.println("性别：" + loginAcc.getSex());
        System.out.println("余额:" + loginAcc.getMoney());
        System.out.println("取现额度：" + loginAcc.getLimit());
    }

    private void register() {
        //创建一个账户对象，用于存储用户输入的注册信息
        Account account = new Account();
        System.out.println("请输入用户名：");
        account.setUserName(sc.next());
        while (true) {
            System.out.println("请输入性别：");
            char sex = sc.next().charAt(0);
            if (sex != '男' && sex != '女') {
                System.out.println("请输入正确的性别！");
                continue;
            }
            account.setSex(sex);
            break;
        }
        while (true) {
            System.out.println("请输入密码：");
            String password = sc.next();
            System.out.println("请确认密码：");
            String confirmPassword = sc.next();
            if (!password.equals(confirmPassword)) {
                System.out.println("两次密码不一致，请重新输入！");
                continue;
            }
            account.setPassword(password);
            break;
        }
        System.out.println("请输入取现额度：");
        account.setLimit(sc.nextDouble());
        //重点：生成一个随机的8位卡号，且不重复
        account.setCardID(createCardID());

        accounts.add(account);
        System.out.println("恭喜" + account.getUserName() + "，开户成功，您的卡号为：" + account.getCardID());
    }

    //生成一个随机的8位卡号，且不重复
    private String createCardID() {
        while (true) {
            StringBuilder cardID = new StringBuilder();
            for (int i = 0; i < 8; i++) {
                cardID.append((int) (Math.random() * 10));
            }
            Account account = queryByCardId(cardID.toString());
            if (account == null) {
                return cardID.toString();
            }
        }
    }

    //根据卡号查询账户
    private Account queryByCardId(String cardID) {
        for (Account account : accounts) {
            if (account.getCardID().equals(cardID))
                return account;
        }
        return null;
    }
}